package com.itheima.controller;

import com.itheima.common.entity.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/validatecode")
@Slf4j
public class UservalidatecodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    @PostMapping("/send4Order")
    public Result send4Order(String telephone) {

        if (telephone != null) {
            //生成验证码
            String code = (int)((Math.random() + 1) * 100000) + "";
            log.info("{}的验证码：{}",telephone,code);
            //将验证码存入redis, 设置过期时间5分钟
            redisTemplate.opsForValue().set(telephone, code, 5 * 60, java.util.concurrent.TimeUnit.SECONDS);
        }

        return new Result(true, "验证码发送成功",null);
    }

    @GetMapping("/send4Login")
    public Result send4Login(String telephone) {

        if (telephone != null) {
            //生成验证码
            String code = (int)((Math.random() + 1) * 100000) + "";
            log.info("{}的验证码：{}",telephone,code);
            //将验证码存入redis, 设置过期时间5分钟
            redisTemplate.opsForValue().set(telephone, code, 5 * 60, java.util.concurrent.TimeUnit.SECONDS);
        }

        return new Result(true, "验证码发送成功",null);
    }

}
